package org.rbreznak.tronco.client;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author Bob Breznak
 *
 */
public class SwipeReader extends KeyAdapter {
	private String cardNumber="";
	private boolean read;
	private boolean reading;
	
	private static char STARTCHAR = ';';
	private static char ENDCHAR = '?';
	private static char LASTCHAR = (char) 13;
	
	public SwipeReader()
	{
		read = true;
		reading = true;
	}
	
	public boolean isEnd() {
		return reading;
	}

	public void keyTyped(KeyEvent e) 
	{
		if(read) {
			if(e.getKeyCode() > 30) {
				if(e.getKeyChar() == ';') {
					cardNumber = "";
					reading = true;
				} else if(e.getKeyChar() == '?') {
					reading = false;
				} else {
					if(reading && Character.isDigit(e.getKeyChar())) {
						cardNumber += e.getKeyChar();
					}
				}
			}
		}
	}
	
	public void clear() {
		cardNumber="";
		read = true;
		reading = false;
	}
	
	public void setRead(boolean run) {
		this.read = run;
	}
	
	public String toString() {
		return cardNumber;
	}
	
	public void keyPressed(KeyEvent e) 
	{
		
	} 
	
	public void keyReleased(KeyEvent e) 
	{

	}
}
